<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleCreate"
          >&emsp;新增&emsp;
          </el-button
          >
        </el-row>
      </div>
      <div v-loading="listLoading" class="item">
        <el-table
          ref="dataList"
          :data="menuList"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          border
          row-key="id"
          style="width: 100%; margin-bottom: 20px"
        >
          <!-- <el-table-column type="selection"></el-table-column> -->
          <el-table-column label="序号" prop="id"></el-table-column>
          <el-table-column label="游戏ID" prop="game_id"></el-table-column>
          <el-table-column label="游戏名称" prop="name"></el-table-column>
          <el-table-column label="房间类型" prop="room_type">
            <template slot-scope="{row}">
              {{ row.room_type === 'all' ? '全部' : row.room_type === 'user' ? '个人房间' : '公会房间' }}
            </template>
          </el-table-column>
          
          <el-table-column label="开启状态" prop="is_open">
            <template slot-scope="{row}">
              <el-tag v-if="row.is_open === 1" type="success" size="normal"  effect="dark">开启</el-tag>
              <el-tag v-if="row.is_open === 0" type="warning" size="normal"  effect="dark">关闭</el-tag>
            </template>
          </el-table-column>
          
          <el-table-column label="操作" width="250px">
            <template slot-scope="scope">
              <el-button
                icon="el-icon-edit"
                plain
                type="primary"
                @click="handleUpdate(scope.row)"
              >编辑
              </el-button
              >
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                plain
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <!-- <cus-pagination
          :limit.sync="listQuery.per_page"
          :page.sync="listQuery.page"
          :total="total"
          @pagination="getList"
        /> -->
      </div>
    </el-card>
    
    <!-- 新增编辑弹框 -->
    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose('editRulesForm')"
    >
      <el-form
        ref="editRulesForm"
        :model="editRulesForm"
        :rules="editRulesFormRules"
        class="demo-ruleForm"
        label-width="120px"
      >
        <el-form-item label="游戏名称" prop="name">
          <el-input
            v-model="editRulesForm.name"
            placeholder="请输入游戏名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="标签" prop="room_type">
          <el-select v-model="editRulesForm.room_type" placeholder="请选择" clearable filterable>
            <el-option v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          
        </el-form-item>
        
        <el-form-item label="状态" prop="is_open">
          <el-radio-group v-model="editRulesForm.is_open">
            <el-radio :label="1">开启</el-radio>
            <el-radio :label="0">关闭</el-radio>
          </el-radio-group>
          
        </el-form-item>

        <el-form-item>
          <el-button @click="dialogFormVisible = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm')"
          >提交
          </el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { roomGameList, roomGameAdd, roomGameDel } from '@/api/roomType'

export default {
  data() {
    return {

      confirmBtn: false,
      options: [
        {
          value: 'all',
          label: '全部'
        },
        {
          value: 'user',
          label: '个人房间'
        },
        {
          value: 'guild',
          label: '公会'
        },
      ],
      specialFlagBigList: [],
      // 要修改的当前行的id
      row_id: 0,
      // 选中的黑名单类别
      checkBlackList: [],
      // 表单规则
      editRulesFormRules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        room_type: [
          { required: true, message: '请选择', trigger: 'blur' }
        ]
      },

      editRulesForm: {
        name: undefined,
        room_type: undefined,
        is_open: undefined,
        id: undefined
      },
      dialogStatus: '',
      // dialog的头部信息
      titleMap: {
        update: '编辑',
        create: '添加'
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      limitList: [],
      listQuery: {
        page: 1,
        per_page: 10
      },
      total: 0,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      roomGameList(this.listQuery).then((response) => {
        this.menuList = response.data
        this.listLoading = false
        this.confirmBtn = false
      })
    },
    // 点击新增
    handleCreate() {
      this.resetForm('editRulesForm')
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 点击编辑
    handleUpdate(row) {
      this.editRulesForm = JSON.parse(JSON.stringify(row))

      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    // 重置表单内容
    resetForm(formName) {
      this.$nextTick(() => {
        // this.$refs[formName].resetFields();
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields()
        }
        this.editRulesForm = {
          name: undefined,
          room_type: undefined,
          is_open: undefined,
          id: undefined
        }
      })
    },
    submitForm(formName) {
      if (this.confirmBtn) return
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.confirmBtn = true
          for (let key in this.editRulesForm) {
              this.editRulesForm[key] = this.editRulesForm[key] === '' ? undefined : this.editRulesForm[key]
          }
          roomGameAdd(this.editRulesForm).then((response) => {
            if (response.code === 200) {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                type: 'success',
                message: '操作成功',
              })
            } else {
              this.confirmBtn = false
            }
          })
        } else {
          return false
        }
      })
    },
    handleDelete(row) {
      this.$confirm('确定删除该游戏？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        roomGameDel({
              id: row.id,
          }).then(res => {
              if(res.code === 200) {
                  this.$message({
                      type: 'success',
                      message: '操作成功'
                  })
              }
              this.dialogVisible = false
              this.getList()
          })
      }).catch(() => {
          this.$message({
              type: 'info',
              message: '已取消'
          });
      });
    },
    
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    }
  }
}
</script>

<style scoped>
/deep/ .el-upload-list__item {
  transition : none !important;
}

/deep/ .el-upload-list--picture {
  max-height : 103px !important;
  overflow   : hidden;
}
</style>
